/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import database.DBConnection;
import java.util.ArrayList;
import java.util.HashMap;
import model.Activity;
import model.Event;
import model.Room;


/**
 *
 * @author Martin Mohr Pedersen
 */
public class ActivityDAO {

    public Activity[] getActivitiesForEvent(Event event) {
        int eventId = event.getId();
        String sql = "SELECT * FROM activity WHERE event_id = ?";
        Object[] args = new Object[] {eventId};

        return find(sql, args);
    }

    private Activity[] find(String sql, Object[] args) {
        ArrayList<HashMap<String, Object>> results = DBConnection.getInstance().executeSelect(sql, args);
        
        return unpack(results);
    }

    private Activity[] unpack(ArrayList<HashMap<String, Object>> results) {
        ArrayList<Activity> activies = new ArrayList<Activity>();
        Activity activity;
        Room room;
        RoomDAO roomD = new RoomDAO();
        int max;
        
        for (HashMap<String, Object> row : results) {
            activity = new Activity();
            activity.setId(Integer.parseInt(row.get("id").toString()));
            activity.setDescription(row.get("description").toString());
            activity.setDuration(Integer.parseInt(row.get("duration").toString()));
            
            if(row.get("max_participants") != null) {
                max = Integer.parseInt(row.get("max_participants").toString());
                activity.setMaxParticipants(max);
            }
            
            activity.setName(row.get("name").toString());
            Boolean req = Boolean.valueOf(row.get("requires_signup").toString());
            activity.setRequiresSignup(Boolean.valueOf(req));
            
            java.sql.Timestamp startTime = (java.sql.Timestamp)row.get("start_time");
            activity.setTimeOfStart(new java.util.Date(startTime.getTime()));
            
            int roomId = Integer.parseInt(row.get("room_id").toString());
            room = roomD.getRoom(roomId);
            activity.setRoom(room);
            activies.add(activity);
        }
        return activies.toArray(new Activity[activies.size()]);
    }
    
    
}
